package com.ourteam.product.requirement.service;

import org.apache.commons.lang.ArrayUtils;

import com.ourteam.product.requirement.dao.IProductVersionDAO;
import com.ourteam.product.requirement.dao.ProductRequirementDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductVersion;
import com.ourteam.product.requirement.dao.ProductVersionDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductVersionScope;
import com.ourteam.product.requirement.dao.ProductVersionScopeDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductVersionScopeRelate;
import com.ourteam.product.requirement.dao.ProductVersionScopeRelateDAOQueryBean;
import com.ourteam.product.requirement.dao.ProductVersionStage;
import com.ourteam.product.requirement.dao.ProductVersionStageDAOQueryBean;
import com.ourteam.product.requirement.domain.ProductVersionBean;
import com.ourteam.product.requirement.domain.ProductVersionScopeBean;
import com.ourteam.product.requirement.domain.ProductVersionScopeRelateBean;
import com.ourteam.product.requirement.domain.ProductVersionStageBean;

/**
 *
 * Template Name : 业务服务实现 Template Version : 1 Template Last Modfiy Date : Mon
 * Nov 06 10:49:11 IRKT 2017 Auto Generate Date : 2017-11-23 10:38:04
 *
 */
public class ProductVersionServiceImpl extends AbstractProductVersionService {
	/**
	 * 取得产品版本列表
	 * 
	 * @param aProductDefineId
	 * 
	 * @param parentVersionId
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductVersionBean[] doGetProductVersions(
			final long aProductDefineId, final long parentVersionId) throws Exception {

		ProductVersionDAOQueryBean queryBean = new ProductVersionDAOQueryBean();

		queryBean.createCriteria().andProductDefineIdEqualTo(aProductDefineId)
				.andParentVersionIdEqualTo(parentVersionId);

		return ProductVersionBean.toArray(productVersionDao.query(queryBean));
	}

	/**
	 * 添加产品版本
	 * 
	 * @param productId
	 * 
	 * @param parentVersionId
	 * 
	 * @param aProductVersion
	 * @throws Exception
	 */
	protected void doAddNewProductVersion(final long productId, final long parentVersionId,
			final com.ourteam.product.requirement.domain.ProductVersionBean aProductVersion) throws Exception {

		ProductVersion productVersion = aProductVersion.convertToProductVersion();

		aProductVersion.setProductDefineId(productId);

		aProductVersion.setParentVersionId(parentVersionId);

		if (productVersionDao.checkIsUnique(productVersion) == false) {
			throw new Exception("product version '" + productVersion.getProductVersion() + "' already exist ");
		}

		productVersionDao.insert(productVersion);
	}

	/**
	 * 去除产品版本信息
	 * 
	 * @param aProductVersionId
	 * @throws Exception
	 */
	protected void doRemoveProductVersion(final long aProductVersionId) throws Exception {
		productVersionDao.delete(aProductVersionId);
	}

	/**
	 * 变更产品版本
	 * 
	 * @param aProductVersion
	 * @throws Exception
	 */
	protected void doModifyProductVersion(
			final com.ourteam.product.requirement.domain.ProductVersionBean aProductVersion) throws Exception {

		ProductVersion productVersion = aProductVersion.convertToProductVersion();

		if (productVersionDao.checkIsUnique(productVersion) == false) {
			throw new Exception("product version '" + productVersion.getProductVersion() + "' already exist ");
		}

		productVersionDao.update(productVersion);
	}

	/**
	 * 根据ID取得产品版本
	 * 
	 * @param productVersionId
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductVersionBean doGetProductVersionById(
			final long productVersionId) throws Exception {

		ProductVersion productVersion = productVersionDao.findById(productVersionId);
		return productVersion != null ? new ProductVersionBean(productVersion) : null;
	}

	/**
	 * 取得�?有产品版�?
	 * 
	 * @param productId
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductVersionBean[] doGetAllProductVersions(final long productId)
			throws Exception {

		ProductVersionDAOQueryBean queryBean = new ProductVersionDAOQueryBean();

		queryBean.createCriteria().andProductDefineIdEqualTo(productId);

		queryBean.setSortProperty(IProductVersionDAO.ProductVersion);

		return ProductVersionBean.toArray(productVersionDao.query(queryBean));
	}

	/**
	 *
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductVersionBean[] doQueryProductVersions(
			final com.ourteam.product.requirement.dao.ProductVersionDAOQueryBean queryBean) throws Exception {

		return ProductVersionBean.toArray(productVersionDao.query(queryBean));
	}

	/**
	 *
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected int doQueryProductVersionCount(
			final com.ourteam.product.requirement.dao.ProductVersionDAOQueryBean queryBean) throws Exception {

		return productVersionDao.queryCount(queryBean);
	}

	/**
	 *
	 * 
	 * @param productVersionStage
	 * @throws Exception
	 */
	protected void doAddNewProductVersionStage(
			final com.ourteam.product.requirement.domain.ProductVersionStageBean productVersionStage) throws Exception {

		this.productVersionStageDao.insert(productVersionStage.convertToProductVersionStage());
	}

	/**
	 *
	 * 
	 * @param productVersionStage
	 * @throws Exception
	 */
	protected void doModifyProductVersionStage(
			final com.ourteam.product.requirement.domain.ProductVersionStageBean productVersionStage) throws Exception {

		this.productVersionStageDao.update(productVersionStage.convertToProductVersionStage());
	}

	/**
	 *
	 * 
	 * @param productVersionStageId
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductVersionStageBean doGetProductVersionStageById(
			final long productVersionStageId) throws Exception {

		ProductVersionStage productVersionStage = this.productVersionStageDao.findById(productVersionStageId);

		return productVersionStage != null ? new ProductVersionStageBean(productVersionStage) : null;
	}

	/**
	 *
	 * 
	 * @param productVersionStageIds
	 * @throws Exception
	 */
	protected void doDeleteProductVersionStages(final long[] productVersionStageIds) throws Exception {

		if (ArrayUtils.isEmpty(productVersionStageIds)) {
			return;
		}

		ProductVersionStageDAOQueryBean queryBean = new ProductVersionStageDAOQueryBean();

		queryBean.createCriteria().andIdIn(productVersionStageIds);

		this.productVersionStageDao.delete(queryBean);
	}

	/**
	 *
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductVersionStageBean[] doQueryProductVersionStages(
			final com.ourteam.product.requirement.dao.ProductVersionStageDAOQueryBean queryBean) throws Exception {

		return ProductVersionStageBean.toArray(this.productVersionStageDao.query(queryBean));
	}

	/**
	 *
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected int doQueryProductVersionStageCount(
			final com.ourteam.product.requirement.dao.ProductVersionStageDAOQueryBean queryBean) throws Exception {

		return this.productVersionStageDao.queryCount(queryBean);
	}

	/**
	 *
	 * 
	 * @param productVersionScope
	 * @throws Exception
	 */
	protected void doAddNewProductVersionScope(
			final com.ourteam.product.requirement.domain.ProductVersionScopeBean productVersionScope) throws Exception {

		this.productVersionScopeDao.insert(productVersionScope.convertToProductVersionScope());
	}

	/**
	 *
	 * 
	 * @param productVersionScope
	 * @throws Exception
	 */
	protected void doModifyProductVersionScope(
			final com.ourteam.product.requirement.domain.ProductVersionScopeBean productVersionScope) throws Exception {

		this.productVersionScopeDao.update(productVersionScope.convertToProductVersionScope());
	}

	/**
	 *
	 * 
	 * @param productVersionScopeIds
	 * @throws Exception
	 */
	protected void doDeleteProductVersionScopes(final long[] productVersionScopeIds) throws Exception {

		if (ArrayUtils.isEmpty(productVersionScopeIds)) {
			return;
		}

		ProductVersionScopeDAOQueryBean queryBean = new ProductVersionScopeDAOQueryBean();

		queryBean.createCriteria().andIdIn(productVersionScopeIds);

		this.productVersionScopeDao.delete(queryBean);
	}

	/**
	 *
	 * 
	 * @param productVersionScopeId
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductVersionScopeBean doGetProductVersionScopeById(
			final long productVersionScopeId) throws Exception {

		ProductVersionScope productVersionScope = this.productVersionScopeDao.findById(productVersionScopeId);

		return productVersionScope != null ? new ProductVersionScopeBean(productVersionScope) : null;
	}

	/**
	 *
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductVersionScopeBean[] doQueryProductVersionScopes(
			final com.ourteam.product.requirement.dao.ProductVersionScopeDAOQueryBean queryBean) throws Exception {

		return ProductVersionScopeBean.toArray(this.productVersionScopeDao.query(queryBean));
	}

	/**
	 *
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected int doQueryProductVersionScopeCount(
			final com.ourteam.product.requirement.dao.ProductVersionScopeDAOQueryBean queryBean) throws Exception {

		return this.productVersionScopeDao.queryCount(queryBean);
	}

	/**
	 *
	 * 
	 * @param productVersionScopseId
	 * 
	 * @param productRequirementIds
	 * @throws Exception
	 */
	protected void doAddProductRequirementVersionScopeRelate(final long productVersionScopseId,
			final long[] productRequirementIds) throws Exception {

		ProductVersionScopeRelate[] scopeRelates = new ProductVersionScopeRelate[productRequirementIds.length];

		for (int i = 0; i < scopeRelates.length; i++) {
			scopeRelates[i] = new ProductVersionScopeRelate();

			scopeRelates[i].setVersionScopeId(productVersionScopseId);
			scopeRelates[i].setRelateType("REQUIREMENT");
			scopeRelates[i].setRelatedObjectId(productRequirementIds[i]);
		}

		this.productVersionScopeRelateDao.batchInsert(scopeRelates);
	}

	/**
	 *
	 * 
	 * @param productVersionScopseId
	 * 
	 * @param productRequirementIds
	 * @throws Exception
	 */
	protected void doRemoveProductRequirementVersionScopeRelate(final long productVersionScopseId,
			final long[] productRequirementIds) throws Exception {

		ProductVersionScopeRelateDAOQueryBean queryBean = new ProductVersionScopeRelateDAOQueryBean();

		queryBean.createCriteria().andVersionScopeIdEqualTo(productVersionScopseId)
				.andRelatedObjectIdIn(productRequirementIds).andRelateTypeEqualTo("REQUIREMENT");

		this.productVersionScopeRelateDao.delete(queryBean);
	}

	/**
	 *
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductRequirementBean[] doQueryVersionScopeRelateProductRequirements(
			final com.ourteam.product.requirement.dao.ProductVersionScopeRelateDAOQueryBean queryBean)
			throws Exception {

		queryBean.setStart(-1);
		queryBean.setMaxResults(-1);

		ProductRequirementDAOQueryBean requirementDAOQueryBean = new ProductRequirementDAOQueryBean();

		queryBean.clearSelectPropertie();
		queryBean.addRelatedObjectIdSelectProperty();

		requirementDAOQueryBean.createCriteria().andIdIn(queryBean, productVersionScopeRelateDao);

		return productService.queryProductRequirements(requirementDAOQueryBean);
	}

	/**
	 *
	 * 
	 * @param queryBean
	 * @throws Exception
	 */
	protected int doQueryProductVersionScopeRelateRequirementCount(
			final com.ourteam.product.requirement.dao.ProductVersionScopeRelateDAOQueryBean queryBean)
			throws Exception {

		queryBean.setStart(-1);
		queryBean.setMaxResults(-1);

		ProductRequirementDAOQueryBean requirementDAOQueryBean = new ProductRequirementDAOQueryBean();

		queryBean.clearSelectPropertie();
		queryBean.addRelatedObjectIdSelectProperty();

		requirementDAOQueryBean.createCriteria().andIdIn(queryBean, productVersionScopeRelateDao);

		return productService.queryProductRequirementCount(requirementDAOQueryBean);
	}

	/**
	 *
	 * 
	 * @param id
	 * @throws Exception
	 */
	protected com.ourteam.product.requirement.domain.ProductVersionScopeRelateBean doGetProductRequirementVersionScopeRelateById(
			final long id) throws Exception {
		ProductVersionScopeRelate scopeRelate = this.productVersionScopeRelateDao.findById(id);

		return scopeRelate != null ? new ProductVersionScopeRelateBean(scopeRelate) : null;
	}
}
